package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"learn-hub/cache"
	"learn-hub/model"
	"strconv"
	"time"
)

func RepoAdd(c *gin.Context) {
	rep_code := c.PostForm("rep_code")
	repo_name := c.PostForm("repo_name")
	if rep_code == "" || repo_name == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := model.Repo{
		RepCode:  rep_code,
		RepoName: repo_name,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}

func RepoOrderAdd(c *gin.Context) {
	rep_id, _ := strconv.Atoi(c.PostForm("rep_id"))
	order_code := c.PostForm("order_code")
	good_num, _ := strconv.Atoi(c.PostForm("good_num"))
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))
	if rep_id == 0 || order_code == "" || good_num == 0 || order_price == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不为空",
		})
		return
	}
	data := model.RepoOrder{
		RepId:      rep_id,
		OrderCode:  order_code,
		GoodNum:    good_num,
		OrderPrice: order_price,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}

func RepoOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var list []model.RepoOrderList
	/*
		SELECT
		  repo_orders.id as OrderId,
		  repos.rep_code as RepCode,
		  repos.repo_name as RepoName,
		  repo_orders.order_code as OrderCode,
		  repo_orders.good_num as GoodNum,
		  repo_orders.order_price as OrderPrice
		FROM repo_orders
		LEFT JOIN repos ON repos.id = repo_orders.rep_id
	*/
	redis_key := "repo_order_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("repo_orders").
			Select(
				"repo_orders.id as OrderId",
				"repos.rep_code as RepCode",
				"repos.repo_name as RepoName",
				"repo_orders.order_code as OrderCode",
				"repo_orders.good_num as GoodNum",
				"repo_orders.order_price as OrderPrice",
			).
			Joins("LEFT JOIN repos ON repos.id = repo_orders.rep_id").
			Offset(offset).
			Limit(size).
			Debug().
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, string(jsonData), time.Hour)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}

func RepoOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var repoOrder model.RepoOrderList
	redis_key := "repo_order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("repo_orders").
			Select(
				"repo_orders.id as OrderId",
				"repos.rep_code as RepCode",
				"repos.repo_name as RepoName",
				"repo_orders.order_code as OrderCode",
				"repo_orders.good_num as GoodNum",
				"repo_orders.order_price as OrderPrice",
			).
			Joins("LEFT JOIN repos ON repos.id = repo_orders.rep_id").
			Where("repo_orders)id = ?", id).
			Debug().
			Find(&repoOrder)
		jsonData, _ := json.Marshal(repoOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, string(jsonData), time.Hour)
	} else {
		json.Unmarshal([]byte(cacheData), &repoOrder)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": repoOrder,
	})
	return
}
